<?php
namespace app\admin\controller;
use app\admin\controller\Base;
/**
 * 角色管理类
 * @author xiaoxiaoping
 *
 */
class Role extends Base{
	/**
	 * 角色列表
	 */
	public function role_list(){
		$roleArr = db('AdminRole')->select();
		$this->assign('role_arr',$roleArr);
		return $this->fetch();
	}
	/**
	 * 添加角色
	 */
	public function role_add(){
		if($this->request->isPost()){
			$requestArr=$this->request->param();
			db('AdminRole')->insert($requestArr);
			$this->success('添加成功！','Role/role_list');
		}
		return $this->fetch();
	}
	//修改角色
	public function role_update(){
		$requestArr=$this->request->param();
		if($this->request->isPost()){
			db('AdminRole')->update($requestArr);
			$this->success('修改成功','role_list');
			exit;
		}
		$role = db('AdminRole')->where('id = ' .$requestArr['id'])->find();
		$this->assign('role',$role);
		return $this->fetch();//输出模板
	}
	//修改权限
	public function update_permission(){
		$requestArr=$this->request->param();
		if($this->request->isPost()){
			$data = array();
			$data['menu_ids'] = implode(',', $requestArr['menu_ids']);
			$data['id'] = $requestArr['id'];
			db('AdminRole')->update($data);
			$this->success("修改成功",'role_list');
			exit;
		}
		$id = $requestArr['id'];
		$obj = db('AdminRole')->where("id = $id")->find();
		$menus = db('AdminMenu')->select();
		$menu_arr_list = array();
		//获取第一级菜单
		foreach ($menus as $m) {
			if ($m['p_id']==0){
				$menu_arr_list[] = $m;
			}
		}
		//获取二级菜单
		foreach ($menu_arr_list as $k=>$m_arr){
			foreach ($menus as $m) {
				if ($m['p_id']==$m_arr['id']){
					$menu_arr_list[$k]['sub_menu'][] = $m;
				}
			}
		}
		$this->assign('menu_arr_list',$menu_arr_list);
		$this->assign('obj',$obj);
		return $this->fetch();
	}
	//删除角色
	public function role_delete(){
		$requestArr=$this->request->param();
		db('AdminRole')->where('id = '.$requestArr['id'])->delete();
		$this->success('删除成功','role_list');
	}
}